#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>
#include <QJsonObject>
#include <QSystemTrayIcon>
#include <QHash>
#include <QCloseEvent>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>

#include "websocketclient.h"
#include "chatwidget.h"
#include "chatitem.h"
#include "message.h"
#include "addfriendswidget.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected :
    void closeEvent(QCloseEvent *event);

signals:
    void signal_signOut();

private slots:
    void slot_signInSuccess(QJsonObject);

    void slot_receiveTextMessage(Message);

    void slot_receiveChatMessage(Message);

    void slot_switchPage();

    void slot_agreeFriendRequestSuccess(QJsonObject);

    void slot_getFriendsReply(QNetworkReply *);

    void on_pushButton_addFriends_clicked();

//    void on_pushButton_startChat_clicked();

    void on_pushButton_groupChat_clicked();

    void on_pushButton_signOut_clicked();

private:
    Ui::MainWindow *ui;
    QString m_userId;
    QString m_username;
    WebSocketClient *m_webSocketClient;
    ChatWidget *m_chatWidget;
    AddFriendsWidget *m_addFriendsWidget;
    QSystemTrayIcon *m_systemTrayIcon;
    QNetworkAccessManager m_manager;
    QNetworkRequest m_request;

    QHash <QString, ChatWidget *> m_chats;
    QHash <QString, ChatItem *> m_chatItems;

    void initSystemTrayIcon();
    ChatItem *addChatItem(QString fromId, Message::Type chatType, QString username);
    void addFriendItem(QJsonObject);
};
#endif // MAINWINDOW_H
